// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import router from "./router";
import Vuex from "vuex";
import store from "./store";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App";
import "@/style/index.scss";
import "@/icon/iconfont.css";
import {
  getToken,
  setToken,
  setSession,
  getSession,
  setUser,
  getUser
} from './util/auth'
import "./util/registerCom"
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
const whiteList = ['/login', '/index']
router.beforeEach((to, from, next) => {
  if (getSession()) {
    if (!store.state.userInfo) {
      store.dispatch('getUserInfo').then(res => {
        if (to.path == '/login') {
          next({
            path: '/index'
          })
        } else {
          next()
        }
      })
    } else {
      if (to.path == '/login') {
        next({
          path: '/index'
        })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      next()
    } else {
      let uri = encodeURIComponent(to.fullPath)
      next(`/login?redirect=${uri}`) // 否则全部重定向到登录页
    }
  }

})

Vue.use(Vuex);
Vue.use(ElementUI);
Vue.config.productionTip = false;
Vue.prototype.$$router = router;
/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  components: {
    App
  },
  template: "<App/>"
});
